package logic

import (
	"context"
	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"
	"house/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListLogic {
	return &HouseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseListLogic) HouseList(in *houserpc.HouseListRequest) (*houserpc.HouseListResponse, error) {
	// todo: add your logic here and delete this line
	page := in.Page
	if page <= 0 {
		page = 1
	}
	pageSize := in.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	var houses []mysql.House
	mysql.DB.Debug().Limit(int(in.Size)).Offset(int(offset)).Order("created_at desc").Find(&houses)
	var HouseList []*houserpc.HouseList
	for _, house := range houses {
		list := houserpc.HouseList{
			Title:     house.Title,
			Lease:     house.Lease,
			Money:     house.Money,
			Deposit:   house.Deposit,
			Area:      int64(house.Area),
			Ancillary: house.Ancillary,
			UserId:    int64(house.UserId),
		}
		HouseList = append(HouseList, &list)
	}
	return &houserpc.HouseListResponse{
		List: HouseList,
	}, nil
}
